package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"pet-manager/cache"
	"pet-manager/models"
	"strconv"
	"time"
)

type CuteController struct {
	beego.Controller
}

func (c *CuteController) Add() {

	img := c.GetString("img")
	name := c.GetString("name")
	attribute := c.GetString("attribute")
	revise := c.GetString("revise")
	create := c.GetString("create")
	cute := models.Cute{
		Img:       img,
		Name:      name,
		Attribute: attribute,
		Revise:    revise,
		Create:    create,
	}
	err := models.DB.Create(&cute).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
		}
		c.ServeJSON()
	}
}
func (c *CuteController) Upload() {

	file, heard, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件获取失败",
		}
		c.ServeJSON()
		return
	}
	if heard.Size > 2*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件过大",
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + heard.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件上传失败",
		}
		c.ServeJSON()
		return
	} else {
		Url := "http://127.0.0.1:8080/" + path
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "文件上传成功",
			Data: Url,
		}
		c.ServeJSON()
	}
}
func (c *CuteController) List() {

	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	offset := (page - 1) * size
	query := models.DB.Model(&models.Cute{})
	var count int64
	query.Count(&count)
	var cute []models.Cute
	cuteList := "cute_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, cuteList).Result()
	if cacheData == "" {
		err := query.Offset(offset).Limit(size).Find(&cute).Error
		jsonData, _ := json.Marshal(cute)
		cache.Rdb.Set(cache.Ctx, cuteList, jsonData, time.Hour*2)
		if err != nil {
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "查询失败",
			}
			c.ServeJSON()
			return
		}

	} else {

		json.Unmarshal([]byte(cacheData), &cute)
	}
	PageData := make(map[string]interface{})
	PageData["count"] = count
	PageData["cute"] = cute
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: PageData,
	}
	c.ServeJSON()
	return
}
func (c *CuteController) Del() {

	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Cute{}, id).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
		}
		c.ServeJSON()
	}
}
func (c *CuteController) Show() {

	id, _ := c.GetInt("id")
	var cute models.Cute
	cuteShow := "cute_shoe" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, cuteShow).Result()
	if cacheJson == "" {
		err := models.DB.Model(&models.Cute{}).Where("id=?", id).First(&cute)
		jsonData, _ := json.Marshal(&cute)
		cache.Rdb.Set(cache.Ctx, cuteShow, jsonData, time.Hour*2)
		if err != nil {
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "查询失败",
			}
			c.ServeJSON()
			return
		}
	} else {
		json.Unmarshal([]byte(cacheJson), &cute)
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: cute,
	}
	c.ServeJSON()

}
